package com.songtai.action.admin.gsjj;

import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.songtai.action.SongtaiActionSupport;
import com.songtai.po.Apply;
import com.songtai.po.JobInfo;
import com.songtai.po.PhotoObj;
import com.songtai.po.PublicObj;
import com.songtai.service.ApplyService;
import com.songtai.service.ImageService;
import com.songtai.service.JobInfoService;
import com.songtai.service.PublicObjService;
import com.songtai.util.Constants;
import com.songtai.util.PageBean;

/**
 * 诚聘英才
 * 
 * @author lx
 * 
 */
public class JobAction extends SongtaiActionSupport {

	private static final long serialVersionUID = 846751411465285666L;

	private PageBean pageBean;

	private int currentPage = 1;

	private String ids;

	// 职位信息
	private JobInfoService jobInfoService;

	// 应聘信息
	private ApplyService applyService;

	private String url;

	private JobInfo jobInfo;

	private Apply apply;

	private String id;

	private String jobName;

	private String codeimg;

	private PublicObj pub;

	private List<PublicObj> qq;

	private PublicObjService publicObjService;

	private Long slider_id = 2L;
	private List<PhotoObj> sliderList;
	private ImageService imageService;

	public List<PhotoObj> getSliderList() {
		return sliderList;
	}

	public void setSliderList(List<PhotoObj> sliderList) {
		this.sliderList = sliderList;
	}

	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public PublicObj getPub() {
		return pub;
	}

	public void setPub(PublicObj pub) {
		this.pub = pub;
	}

	public void setPublicObjService(PublicObjService publicObjService) {
		this.publicObjService = publicObjService;
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	/**
	 * 分页获取全部职位信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getJobInfos() throws Exception {
		if (null == pageBean) {
			pageBean = new PageBean();
		}
		// 设置统计总条数HQL
		pageBean.setCountHql("select count(id) from JobInfo as j");
		// 设置查询所有实体HQL
		pageBean.setEntiesHql("from JobInfo as j");
		// 设置开始查询条数
		pageBean.setStartIndex((currentPage - 1) * 10);
		// 设置查询多少条
		pageBean.setEndIndex(10);
		pageBean.setCurrentPage(currentPage);
		pageBean = jobInfoService.getJobInfos(pageBean);
		return SUCCESS;
	}

	/**
	 * 添加职位信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addJobIno() throws Exception {
		url = "getJobInfos.htm";
		jobInfo.setUpTime(new Date());
		System.out.println("--------------");
		jobInfoService.saveJobInfo(jobInfo);
		System.out.println("++++++++++++");
		return SUCCESS;
	}

	/**
	 * 删除职位信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delJobInos() throws Exception {
		url = "getJobInfos.htm";
		try {
			jobInfoService.delJobInfos(ids);
		} catch (Exception e) {
			addActionError("此职位有人投递简历，请将简历删除后，在删除职位");
			return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 查询职位信息详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getDesc() throws Exception {
		jobInfo = jobInfoService.getJobInfo(id);
		return SUCCESS;
	}

	/**
	 * 跳转到修改页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toUpdate() throws Exception {
		jobInfo = jobInfoService.getJobInfo(id);
		return SUCCESS;
	}

	/**
	 * 修改职位信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateJobIno() throws Exception {
		url = "getJobInfos.htm";
		JobInfo jobInfoTemp = jobInfoService.getJobInfo(id);
		String id = jobInfoTemp.getId();
		BeanUtils.copyProperties(jobInfoTemp, jobInfo);
		jobInfoTemp.setId(id);
		jobInfoTemp.setUpTime(new Date());
		jobInfoService.updateJobInfo(jobInfoTemp);
		return SUCCESS;
	}

	/**
	 * 得到应聘信息集合
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getApplies() throws Exception {
		if (null == pageBean) {
			pageBean = new PageBean();
		}
		// 设置统计总条数HQL
		pageBean
				.setCountHql("select count(id) from Apply as a where a.jobInfo = '"
						+ id + "'");
		// 设置开始查询条数
		pageBean.setStartIndex((currentPage - 1) * 10);
		pageBean.setCurrentPage(currentPage);
		// 设置查询所有实体HQL
		pageBean.setEntiesHql("from Apply as a where a.jobInfo = '" + id + "'");

		// 设置查询多少条
		pageBean.setEndIndex(10);

		applyService.getApplies(pageBean);
		jobInfo = jobInfoService.getJobInfo(id);
		return SUCCESS;
	}

	/**
	 * 保存应聘简历
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveApply() throws Exception {
		if (!session.get(Constants.VALIDATE_CODE).equals(codeimg)) {
			addFieldError("imgerror", "验证码错误");
		}
		jobInfo = jobInfoService.getJobInfo(id);
		apply.setJobInfo(jobInfo);
		apply.setRecordDate(new Date());
		applyService.saveApply(apply);
		return SUCCESS;
	}

	/**
	 * 删除应聘信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delApplies() throws Exception {
		// jobName = URLDecoder.decode(jobName,"UTF-8");
		url = "getApplies.htm?id=" + id;
		applyService.delApply(ids);
		return SUCCESS;
	}

	/**
	 * 查询应聘信息详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getApplyIno() throws Exception {
		apply = applyService.getApply(id);
		return SUCCESS;
	}

	/**
	 * 前台分页职位信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String jobInfos() throws Exception {
		if (null == pageBean) {
			pageBean = new PageBean();
		}
		// 设置统计总条数HQL
		pageBean.setCountHql("select count(id) from JobInfo as j");
		// 设置查询所有实体HQL
		pageBean.setEntiesHql("from JobInfo as j");
		// 设置开始查询条数
		pageBean.setStartIndex((currentPage - 1) * 10);
		// 设置查询多少条
		pageBean.setEndIndex(10);
		pageBean.setCurrentPage(currentPage);
		pageBean = jobInfoService.getJobInfos(pageBean);
		pub = publicObjService.getObjByNid("21");
		qq = publicObjService.getObjsByNid("23");
		sliderList = imageService.getImageByNodeid(slider_id, 10,
				PhotoObj.TYPE_SLIDER);
		return SUCCESS;
	}

	/**
	 * 前台查询职位信息详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String jobInfoDesc() throws Exception {
		jobInfo = jobInfoService.getJobInfo(id);
		pub = publicObjService.getObjByNid("21");
		qq = publicObjService.getObjsByNid("23");
		sliderList = imageService.getImageByNodeid(slider_id, 10,
				PhotoObj.TYPE_SLIDER);
		return SUCCESS;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public JobInfo getJobInfo() {
		return jobInfo;
	}

	public void setJobInfo(JobInfo jobInfo) {
		this.jobInfo = jobInfo;
	}

	public Apply getApply() {
		return apply;
	}

	public void setApply(Apply apply) {
		this.apply = apply;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setJobInfoService(JobInfoService jobInfoService) {
		this.jobInfoService = jobInfoService;
	}

	public void setApplyService(ApplyService applyService) {
		this.applyService = applyService;
	}

	public String getCodeimg() {
		return codeimg;
	}

	public void setCodeimg(String codeimg) {
		this.codeimg = codeimg;
	}

	public List<PublicObj> getQq() {
		return qq;
	}

	public void setQq(List<PublicObj> qq) {
		this.qq = qq;
	}

}
